package com.hzhc.controller.wxapp;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.VehicleReservation;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.UserMemberService;
import com.hzhc.service.VehicleReservationService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.VehicleReservationVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
public class WxApiVehicleController extends BaseController {

    private final VehicleReservationService vehicleReservationService;
    private final UserMemberService memberService;
    @Value("${file.cntrUploadFolder}")
    private String cntrUploadFolder;
    @Value("${file.cntrAccessPath}")
    private String cntrAccessPath;

    /**
     * 车辆预约列表
     */
    @RequestMapping("/vehicleReservationList")
    public ResultUtil<List<VehicleReservationVo>> vehicleReservationList(HttpServletRequest request, @RequestBody String json) {
        Long userID = TokenUtil.getUserID(request);
        String type = memberService.getById(userID).getUserType();

        VehicleReservationVo vo = JSONObject.parseObject(json, VehicleReservationVo.class);
        Page<VehicleReservationVo> page =new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<VehicleReservationVo> list = null;
        if ("1".equals(type)){//货主
            list = vehicleReservationService.getWxList(page, vo);
        }else if ("2".equals(type)){//司机
            list = vehicleReservationService.getDriverList(page, vo);
        }

        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    //wx端司机 车辆预约 提交
    @GetMapping("/updateSubmitType/{id}")
    public ResultUtil<T> updateSubmitType(HttpServletRequest request,@PathVariable Long id){
        UserDto user = TokenUtil.getUser(request);
        return vehicleReservationService.updateSubmitType(user,id);
    }

    //wx端司机 车辆预约 回退
    @GetMapping("/backSubmitType/{id}")
    public ResultUtil<T> backSubmitType(HttpServletRequest request,@PathVariable Long id){
        UserDto user = TokenUtil.getUser(request);
        return vehicleReservationService.backSubmitType(user,id);
    }

    /**
     * 保存重进车辆预约
     */
    @RequestMapping("/addVehicleReservation")
    @SysTemLog(operModul = "微信新增车辆预约信息", operType = "新增", operDesc = "微信新增车辆预约信息", modular = "APP")
    public ResultUtil<T> addVehicleReservation(@RequestBody String json) {
        VehicleReservationVo vo = JSONObject.parseObject(json, VehicleReservationVo.class);
        JSONObject obj = JSONObject.parseObject(json);
        JSONArray cntrPhoto = new JSONArray();
        JSONArray cntrPhotoB = new JSONArray();
        if (obj.getJSONArray("photos") != null) {
            cntrPhoto = obj.getJSONArray("photos");
        }
        if (obj.getJSONArray("photosB") != null) {
            cntrPhotoB = obj.getJSONArray("photosB");
        }
        vo.setMemberId(vo.getMemberId());
        vo.setUserName(vo.getUserName());
        vo.setCompanyId(vo.getCompanyId());
        return vehicleReservationService.addVehicleReservation(vo,cntrPhoto,cntrPhotoB,cntrUploadFolder,cntrAccessPath);
    }

    /**
     * 修改重进车辆预约(重进、提重、空进)
     */
    @RequestMapping("/updateVehicleReservation")
    @SysTemLog(operModul = "微信修改车辆预约息", operType = "修改", operDesc = "微信修改车辆预约息", modular = "APP")
    public ResultUtil<T> updateVehicleReservation(@RequestBody String json) {
        VehicleReservationVo vo = JSONObject.parseObject(json, VehicleReservationVo.class);
        JSONObject obj = JSONObject.parseObject(json);
        JSONArray cntrPhoto = new JSONArray();
        JSONArray cntrPhotoB = new JSONArray();
        if (obj.getJSONArray("photos") != null) {
            cntrPhoto = obj.getJSONArray("photos");
        }
        if (obj.getJSONArray("photosB") != null) {
            cntrPhotoB = obj.getJSONArray("photosB");
        }
        vo.setUpdateMemberId(vo.getMemberId());
        vo.setUpdateUserName(vo.getUserName());
        vo.setCompanyId(vo.getCompanyId());
        return vehicleReservationService.updateVehicleReservation(vo,cntrPhoto,cntrPhotoB,cntrUploadFolder,cntrAccessPath);
    }

    /**
     * 删除重进车辆预约
     */
    @RequestMapping("/delVehicleReservation")
    @SysTemLog(operModul = "微信删除重进车辆预约信息", operType = "删除", operDesc = "微信删除重进车辆预约信息", modular = "APP")
    public ResultUtil<T> delVehicleReservation(@RequestBody String json) {
        VehicleReservation vo = JSONObject.parseObject(json, VehicleReservation.class);
        return vehicleReservationService.delVehicleReservation(vo);
    }

    /**
     * 保存提空重回车辆预约
     */
    @RequestMapping("/addTkzhVehicleReservation")
    @SysTemLog(operModul = "微信新增提空重回车辆预约信息", operType = "新增", operDesc = "微信新增提空重回车辆预约信息", modular = "APP")
    public ResultUtil<T> addTkzhVehicleReservation(@RequestBody String json) {
        VehicleReservationVo vo = JSONObject.parseObject(json, VehicleReservationVo.class);
        vo.setMemberId(vo.getMemberId());
        vo.setUserName(vo.getUserName());
        vo.setCompanyId(vo.getCompanyId());
        return vehicleReservationService.addTkzhVehicleReservation(vo);
    }

    /**
     * 修改提空重回车辆预约
     */
    @RequestMapping("/updateTkzhVehicleReservation")
    @SysTemLog(operModul = "微信修改提空重回车辆预约息", operType = "修改", operDesc = "微信修改提空重回车辆预约息", modular = "APP")
    public ResultUtil<T> updateTkzhVehicleReservation(@RequestBody String json) {
        VehicleReservationVo vo = JSONObject.parseObject(json, VehicleReservationVo.class);
        vo.setMemberId(vo.getMemberId());
        vo.setUserName(vo.getUserName());
        vo.setCompanyId(vo.getCompanyId());
        return vehicleReservationService.updateTkzhVehicleReservation(vo);
    }

    /**
     * 删除提空重回车辆预约
     */
    @RequestMapping("/delTkzhVehicleReservation")
    @SysTemLog(operModul = "微信删除提空重回车辆预约信息", operType = "删除", operDesc = "微信删除提空重回车辆预约信息", modular = "APP")
    public ResultUtil<T> delTkzhVehicleReservation(@RequestBody String json) {
        VehicleReservation vo = JSONObject.parseObject(json, VehicleReservation.class);
        return vehicleReservationService.delTkzhVehicleReservation(vo);
    }


    /**
     * 中控理货 车辆预约数据
     */
    @RequestMapping("/getVehicleList")
    public ResultUtil<List<VehicleReservationVo>> getVehicleList(@RequestBody VehicleReservationVo vo) {
        Page<VehicleReservationVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<VehicleReservationVo> vehicleReservationVos = vehicleReservationService.getVehicleList(page, vo);
        return ResultUtil.build(vehicleReservationVos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }
}
